/*
 * ArCoLIVE - Live Internet Videoconference Environment
 * Information Technology Departament - Federal University of Alagoas - Brazil
 *
 * Copyright (C) 2005 Leandro Melo de Sales (leandro@labpesquisas.tci.ufal.br)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package br.ufal.tci.nexos.arcolive.service.connection;

import java.util.Hashtable;

import br.ufal.tci.nexos.arcolive.events.ParticipantConnectionEvent;
import br.ufal.tci.nexos.arcolive.exception.ArCoLIVECannotProcessCommandException;
import br.ufal.tci.nexos.arcolive.exception.ArCoLIVEException;
import br.ufal.tci.nexos.arcolive.participant.ArCoLIVEParticipant;

/**
 * CLASSNAME.java
 *
 * CLASS DESCRIPTION
 *
 * @see CLASSNAME
 *
 * @author <a href="mailto:felipe@labpesquisas.tci.ufal.br">Felipe Barros Pontes</a>.
 * @author <a href="mailto:leandro@labpesquisas.tci.ufal.br">Leandro Melo de Sales</a>.
 * @since 0.1
 * @version 0.1
 *
 * <p><b>Revisions:</b>
 *
 * <p><b>yyyymmdd USERNAME:</b>
 * <ul>
 * <li> VERSION
 * </ul>
 */

public class ConnectionRequest {

	private Object owner;
	private int protocolId;
	private Hashtable parameters;
	private String rawCommand = "";
	private boolean parserResult;
	
	public ConnectionRequest(ParticipantConnectionEvent participantEvent) throws ArCoLIVECannotProcessCommandException {
		this.owner      = participantEvent.getSource();
		this.rawCommand = participantEvent.getStringRequest();
		this.parameters = new Hashtable();
		if (!this.processInput()) {
			throw new ArCoLIVECannotProcessCommandException("Error while process command syntax: " + this.rawCommand);
		}
	}
	
	public ConnectionRequest(ArCoLIVEParticipant participant) {
		this.owner = participant;
		this.parameters = new Hashtable();
	}
	
	public Object getOwner() {
		return this.owner;
	}
	
	public int getProtocolId() {
		return this.protocolId;
	}
	
	public void addParameter(String key, String parameter) {
		this.parameters.put(key, parameter);
	}
	
	public void removeParameter(String key) {
		this.parameters.remove(key);
	}
	
	public boolean containParameter(String parameter) {
		return this.parameters.containsKey(parameter);
	}
	
	public String getStringParameter(String key) {
		Object parameter = this.parameters.get(key);

		return ((parameter != null) ? (String) parameter : "");
	}
	
	public String getSafelyStringParameter(String key) throws ArCoLIVEException {
		if (this.parameters.containsKey(key)) {
			return (String)this.parameters.get(key);
		} else {
			throw new ArCoLIVEException("Key not found or cast error");
		}
	}
	
	public long getLongParameter(String key) {
		return Long.parseLong((String)this.parameters.get(key));
	}
	
	public long getSafelyLongParameter(String key) throws ArCoLIVEException {
		try {
			return Long.parseLong((String)this.parameters.get(key));
		} catch (Exception e) {
			throw new ArCoLIVEException("Key not found or cast error");
		}
	}
	
	public Integer getIntegerParameter(String key) {
		
		return new Integer(Integer.parseInt((String)this.parameters.get(key)));
	}
	
	public Integer getSafelyIntegerParameter(String key) throws ArCoLIVEException {
		try {
			return new Integer(Integer.parseInt((String)this.parameters.get(key)));
		} catch (Exception e) {
			throw new ArCoLIVEException("Key not found or cast error");
		}
	}
	
	public void setParserResult(boolean parserResult) {
		this.parserResult = parserResult;
	}
	
	public void setProtocolId(int protocolId) {
		this.protocolId = protocolId;
	}

	public String getRawCommand() {
		return this.rawCommand;
	}
	
	public boolean getParserResult() {
		return this.parserResult;
	}

	public Hashtable getParameters() {
		return parameters;
	}
	
	private boolean processInput() {
		int commandCode = 0;
		
		if (ConnectionRequest.parseCommand(this.rawCommand)) {
			
			String messageTokens[] = rawCommand.split("#");
			commandCode = Integer.parseInt(messageTokens[0]);
			this.setProtocolId(commandCode);
			
			for (int i = 1; i < messageTokens.length; i++) {
				String keyparameter[] = messageTokens[i].split("=");
				this.addParameter(keyparameter[0], keyparameter[1]);
			}
			
			return true;
			
		}
		return false;
		
	}
	
	private static boolean parseCommand(String command) {
		String regex1 = "[0-9]{3}(#requestId=([0-9])+){1}(#commandId=([0-9])+){1}(#[a-zA-Z0-9]+[=][a-zA-Z0-9]+)*";
		//String regex2 = "[0-9]{3}(#[a-zA-Z0-9]+[=][a-zA-Z0-9]+)+(#commandId=([0-9])+){1}";
		//return (command.matches(regex1) || command.matches(regex2));
		return command.matches(regex1);
	}
	
	public static void main(String[] args) {
		if (parseCommand("700#requestId=1#commandId=701")) {
			System.out.println("OK");
		} else {
			System.out.println("ERR");
		}
	}
	
}